package com.htl.exampro.question.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htl.exampro.base.utils.UserUtil;
import com.htl.exampro.question.mapper.QuestionTypeMapper;
import com.htl.exampro.question.pojo.domain.QuestionTypeDO;
import com.htl.exampro.question.pojo.vo.QuestionTypeVO;
import com.htl.exampro.question.service.QuestionTypeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 题型代码表 服务实现类
 * </p>
 *
 * @author hehehe
 * @since 2025-05-25
 */
@Service
public class QuestionTypeServiceImpl extends ServiceImpl<QuestionTypeMapper, QuestionTypeDO> implements QuestionTypeService {

    @Override
    public List<QuestionTypeVO> queryAllQuestionType() {
        LambdaQueryWrapper<QuestionTypeDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(QuestionTypeDO::getTypeId, QuestionTypeDO::getTypeName);
        queryWrapper.eq(QuestionTypeDO::getCreatedPerson, UserUtil.getUserId())
                .or().isNull(QuestionTypeDO::getCreatedPerson);
        List<QuestionTypeDO> questionTypeDOS = this.baseMapper.selectList(queryWrapper);
        return BeanUtil.copyToList(questionTypeDOS, QuestionTypeVO.class);
    }

    @Override
    public int saveQuestionType(QuestionTypeVO questionTypeVO) {
        QuestionTypeDO questionTypeDO = BeanUtil.toBean(questionTypeVO, QuestionTypeDO.class);
        return this.baseMapper.insert(questionTypeDO);
    }
}
